const state = {
  cart: uni.getStorageSync('addCate') || []
}
const mutations = {
  removeGoods(state,data){
    const newArr = state.cart.filter(item => item.goods_id !== +data)
    state.cart = newArr
    uni.setStorageSync('addCate',state.cart)
  },
  changCount(state,data){
    const newObj = state.cart.find(item=> item.goods_id === data.goods_id)
    if(newObj){   
    newObj.goods_count = data.goods_count
    }
    uni.setStorageSync('addCate',state.cart)
  },
  addcateList (state,data) {
    const newObj = state.cart.find(item=> item.goods_id === data.goods_id)
    if(!newObj){
      state.cart.push(data)
    }else{
      newObj.goods_count += 1
    }
    // console.log(state.cart);
    uni.setStorageSync('addCate',state.cart)
  },
  changeStatus(state,data){
    const newObj = state.cart.find(item=> item.goods_id === data.goods_id)
    newObj.goods_status = data.goods_status
    uni.setStorageSync('addCate',state.cart)
  },
  // 购物车全选按钮反选
  changeAllStatus(state,data){
    state.cart.forEach(item => {
      item.goods_status = data
    })
   uni.setStorageSync('addCate',state.cart)
  }
}
const actions = {}
const getters = {
  changeInfo(state){
 return state.cart.reduce((sum,item)=>{
    return sum += item.goods_status ? item.goods_count :0
   },0)
   
  },
  // 单选全选
  isCheck(state){
   return state.cart.every(item =>item.goods_status)
  },
  totalPrice(){
    const res =  state.cart.reduce((sum,item)=>{
      return sum += item.goods_status ? item.goods_count * item.goods_price :0
     },0)
     return res
  }
}
export default {
  namespaced:true,
  state,
  mutations,
  actions,
  getters
}